<?php
/**
 * Handles the loading of a view controller
 *
 * @package Concerto
 * @subpackage Initializers
 * @author Jason Raede <jason@torchdm.com>
 */
namespace application\initializers;

class Loader {

	/**
	 * Initializes and runs the load() method on the view controller if authorized
	 */
	public function load() {
		global $concerto;
		switch(getQueryVar('view')) {
			case 'admin':
				$loader = new \application\views\Admin;
				break;
			case 'form':
				$loader = new \application\views\Form;
				break;
			case 'json':
			case 'API':
			case 'api':
				$loader = new \application\views\API;
				break;
			case 'rss':
				$loader = new \application\views\RSS;
				break;
			case 'handler':
				$loader = new \application\views\Handler;
				break;
			case 'image':
				$loader = new \application\views\Image;
				break;
			case 'file':
				$loader = new \application\views\File;
				break;
			case 'template':
			default:
				$loader = new \application\views\Template;
				break;
		}
		if($loader->authorized()) {
			$loader->load();
		}
		else {
			if($loader->handleErrors) {
				$loader->handleError(ERROR_UNAUTHORIZED);
			}
			else {
				$this->loadError(ERROR_UNAUTHORIZED);
			}
		}
	}
	
	/**
	 * Temporary error handler, initializes the Error view controller and loads it with the
	 * given error
	 */
	public function loadError($error) {
		$loader = new \application\views\Error;
		$loader->load($error);
	}

	
}
?>
